package com.lagou.controller;

import com.lagou.dao.Dao;
import com.lagou.util.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;


public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username + "----------" + password);
        QueryRunner qr = new QueryRunner();
        Connection con = DruidUtils.getConnection();
        Dao dao = new Dao(qr,con);
        try {
           if (dao.finadAdmin(username,password)){
                //resp.getWriter().write("<h>登入成功</h>");
               HttpSession session = req.getSession();
               session.setAttribute("admin",username);
              // resp.sendRedirect("/Login_demo/add.html");
               resp.sendRedirect("/Login_demo/add.jsp");
           }else{
               resp.getWriter().write("<h>用户名或密码错误<h>");
           }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dao.flash();
        }
        //resp.getWriter().write("????????");
    }
}
